﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// 服务端的主处理客户的服务
    /// </summary>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ACESocketHandlerManagerAtServer
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 当前的主处理的指针
        /// </summary>
        private IntPtr m_Value      = IntPtr.Zero;
        #endregion

        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 全部连接过的客户端的数量
        /// </summary>
        public int ClientCount
        {
            get { return ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_ClientCount(m_Value); }
        }

        /// <summary>
        /// 在线客户端的数量
        /// </summary>
        public int OnlineClientCount
        {
            get { return ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_OnlineClientCount(m_Value); }
        }

        /// <summary>
        /// 给出某个客户端
        /// </summary>
        /// <param name="iIndex">索引</param>
        /// <returns></returns>
        public ACEClientHandlerAtServer this[int iIndex]
        {
            get
            {
                IntPtr m_newValue = IntPtr.Zero;

                if (ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_GetClientHandler(iIndex, ref m_newValue, m_Value) == false)
                    return null;

                if (m_newValue == IntPtr.Zero)
                    return null;

                ACEClientHandlerAtServer pClientHandlerAtServer = new ACEClientHandlerAtServer();
                pClientHandlerAtServer.Value                    = m_newValue;

                return pClientHandlerAtServer;
            }
        }

        /// <summary>
        /// 给出某个客户端
        /// </summary>
        /// <param name="strIndex">地址和端口</param>
        /// <returns></returns>
        public ACEClientHandlerAtServer this[string strIndex]
        {
            get
            {
                IntPtr m_newValue = IntPtr.Zero;

                if (ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_GetClientHandler2(strIndex, ref m_newValue, m_Value) == false)
                    return null;

                if (m_newValue == IntPtr.Zero)
                    return null;

                ACEClientHandlerAtServer pClientHandlerAtServer = new ACEClientHandlerAtServer();
                pClientHandlerAtServer.Value                    = m_newValue;

                return pClientHandlerAtServer;
            }
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 发送数据到全部的客户端
        /// </summary>
        /// <param name="sendMessageBlock"></param>
        public void SendToAll(ACEMessageBlock sendMessageBlock)
        {
            if ( sendMessageBlock == null )
            {
                Debug.WriteLine( "ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_SendToAll(...) - sendMessageBlock == null error!" );

                return;
            }

            if ( ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_SendToAll( sendMessageBlock.Value, m_Value ) == false )
            {
                Debug.WriteLine( "ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_SendToAll(...) - ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_SendToAll(...) == false error!" );

                return;
            }

            // 释放入内存池
            ACESocketHandlerManagerAtServer.s_InternalMessageBlockPool.ReleasePoolContent(sendMessageBlock);
        }

        #region zh-CHS 私有静态成员变量 | en Private Static Member Variables
        /// <summary>
        /// 为优化而采用内存池的数据区
        /// </summary>
        static internal ACEProcessMessageBlockPool<ACEMessageBlock> s_InternalMessageBlockPool = new ACEProcessMessageBlockPool<ACEMessageBlock>( "ACESocketHandlerManagerAtServer - ACEMessageBlock", 2048 );
        #endregion
        /// <summary>
        /// 给出新的数据区
        /// </summary>
        /// <returns></returns>
        public ACEMessageBlock GetNewSendMessageBlock()
        {
            IntPtr m_newMessageBlockValue = IntPtr.Zero;
            if ( ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_GetNewSendMessageBlock( ref m_newMessageBlockValue, m_Value ) == false )
            {
                Debug.WriteLine( "ACESocketHandlerManagerAtServer.GetNewSendMessageBlock(...) - ACESocketHandlerManagerAtServer.ACESocketHandlerManagerAtServer_GetNewSendMessageBlock(...) == false error!" );

                return null;
            }

            if ( m_newMessageBlockValue == IntPtr.Zero )
            {
                Debug.WriteLine( "ACESocketHandlerManagerAtServer.GetNewSendMessageBlock(...) - m_newMessageBlockValue == IntPtr.Zero error!" );

                return null;
            }

            // 从内存池获取内存 - 调用太频繁内存池优化
            ACEMessageBlock newSendMessageBlock = ACESocketHandlerManagerAtServer.s_InternalMessageBlockPool.AcquirePoolContent();
            newSendMessageBlock.Value           = m_newMessageBlockValue;

            return newSendMessageBlock;
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 主处理的指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ACESocketHandlerManagerAtServer_ClientCount(IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ACESocketHandlerManagerAtServer_OnlineClientCount(IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="pClientHandlerAtServer"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtServer_GetClientHandler(int iIndex, ref IntPtr pClientHandlerAtServer, IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="strIndex"></param>
        /// <param name="pClientHandlerAtServer"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtServer_GetClientHandler2(string strIndex, ref IntPtr pClientHandlerAtServer, IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtServer_SendToAll(IntPtr pMessageBlock, IntPtr pSocketHandlerManagerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pNewSendMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACESocketHandlerManagerAtServer_GetNewSendMessageBlock(ref IntPtr pNewSendMessageBlock, IntPtr pSocketHandlerManagerAtServer);
        #endregion
    }
}
#endregion

